﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using AccessControl.Common;
using AccessControl.Common.Enumerations;
using AccessControl.Utility.EnumHelper;
using AccessControl.Utility.Tools;
using AccessControl.WorkflowEngine;


namespace ACTest.WorkflowTester
{
    public partial class frmWorkflowTest : Form{
        private List<WorkflowEngine> WorkflowList;

        public frmWorkflowTest()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LogFile.WriteLog("log\\log.txt", eventLevelConstants.Error, "application started");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = true;
            btnStart.Enabled = false;

            Start();

        }

        public void Start()
        {
            WorkflowEngine.Initialize();

            if (WorkflowEngine.tblWorklow == null) return;
            WorkflowList = new List<WorkflowEngine>();

            foreach (var workflow in from AccessControlDS.tblWorkflowRow wf in WorkflowEngine.tblWorklow.Rows select new WorkflowEngine(wf))
            {
                WorkflowList.Add(workflow);
                workflow.DeviceConnectionChanged += Workflow_DeviceConnectionChanged;
                workflow.DataTransfered += Workflow_DataTransfered;
                workflow.EventOccured += EventOccured;
                workflow.StartAsync();
            }
        }


        private void EventOccured(AccessControlDS.tblWorkflowRow sender,AccessControlDS.tblEventLogDataTable tbl){
            var row = (AccessControlDS.tblEventLogRow) tbl.Rows[0];

            Invoke(
                new MethodInvoker(
                    () => txtLog.AppendText(sender.WorkflowName + ": " + row.pkEventLog+ " " + row.fkPerson + " " + ((EventResultConstants)row.Result).GetDescription() + " " + row.Reason + "\n" + Environment.NewLine)));
        }

        private void btnStop_Click(object sender, EventArgs e){
            Stop();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void Workflow_DataTransfered(object sender,string data)
        {
            Invoke(
                new MethodInvoker(
                    () => txtData.AppendText(data + "\n")));
        }

        private void Workflow_DeviceConnectionChanged(AccessControlDS.tblWorkflowRow workflow, DeviceConnectionChangedEventArgs e)
        {
            Invoke(
                new MethodInvoker(
                    () => txtLog.AppendText("DeviceID " +e.PkDevice + " " + (e.Connected?"Connected":"Disconnected")+"\n")));
        }

        internal void Stop()
        {
            foreach (WorkflowEngine workflow in WorkflowList)
            {
                workflow.End();
            }
        }

        private void btnClearData_Click(object sender, EventArgs e){
            txtData.Text = "";
        }


        private void btnClear_Click(object sender, EventArgs e){
            txtLog.Text = "";
        }

        private void frmWorkflowTest_FormClosed(object sender, FormClosedEventArgs e)
        {
            Stop();
            Thread.CurrentThread.Abort();
        }

        private void btnOpenGate_Click(object sender, EventArgs e)
        {
            WorkflowList[0].VaKonGateSabmordaro();
        }

    }

}
